# Contributor: prymeroot <pryme@gmx.com>
# Contributor: Jeff Bilyk <jbilyk@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=fail2ban
pkgver=0.11.2
pkgrel=2
pkgdesc="Scans log files for login failures then updates iptables to reject originating ip address"
url="https://www.fail2ban.org/"
arch="noarch"
license="GPL-2.0-or-later"
depends="python3 iptables ip6tables logrotate"
makedepends="python3-dev py3-setuptools bash"
subpackages="$pkgname-tests $pkgname-doc $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/fail2ban/fail2ban/archive/$pkgver.tar.gz
	$pkgname-747d468-fix-tests.patch::https://github.com/fail2ban/fail2ban/commit/747d4683221b5584f9663695fb48145689b42ceb.patch
	CVE-2021-32749.patch
	python-3.10.patch
	setuptools59.patch

	fail2ban.confd
	fail2ban.logrotate
	alpine-ssh.jaild
	alpine-sshd.filterd
	alpine-sshd-ddos.filterd
	"
case "$CARCH" in
	s390x|mips64|armhf) options="!check";;
esac

# secfixes:
#   0.11.2-r2:
#     - CVE-2021-32749

build() {
	sh fail2ban-2to3
	python3 setup.py build
}

check() {
	python3 setup.py test
}

package() {
	python3 setup.py install --root "$pkgdir"

	install -Dm755 files/gentoo-initd "$pkgdir"/etc/init.d/fail2ban
	install -Dm644 "$srcdir"/fail2ban.confd "$pkgdir"/etc/conf.d/fail2ban
	install -Dm644 "$srcdir"/fail2ban.logrotate \
		"$pkgdir"/etc/logrotate.d/fail2ban
	install -Dm644 "$srcdir"/alpine-ssh.jaild \
		"$pkgdir"/etc/fail2ban/jail.d/alpine-ssh.conf
	install -Dm644 "$srcdir"/alpine-sshd.filterd \
		"$pkgdir"/etc/fail2ban/filter.d/alpine-sshd.conf
	install -Dm644 "$srcdir"/alpine-sshd-ddos.filterd \
		"$pkgdir"/etc/fail2ban/filter.d/alpine-sshd-ddos.conf

	chmod o+r "$pkgdir"/usr/lib/python3*/site-packages/fail2ban*.egg-info/*

	install -Dm644 -t "$pkgdir"/usr/share/man/man1 man/*.1
	install -Dm644 -t "$pkgdir"/usr/share/man/man5 man/*.5

	# TODO: Create paths-alpine.conf and remove paths-debian.conf.
	find "$pkgdir"/etc/fail2ban/ -name 'paths-*.conf' \
		-a ! -name 'paths-common.conf' \
		-a ! -name 'paths-debian.conf' \
		-delete
}

tests() {
	pkgdesc="Fail2ban test cases"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/bin/fail2ban-testcases
	amove usr/lib/python3.*/site-packages/fail2ban/tests
}

sha512sums="
46b27abd947b00ea64106dbac563ef8afef38eec86684024d47d9a0e8c1969ff864ad6df7f4f8de2aa3eb1af6d769fb6796592d9f0e35521d5f95f17b8cade97  fail2ban-0.11.2.tar.gz
5c0748c048031d88bc8fd2519bf99a35437b78a08fa942dbccdd2c0e4e9125560a847a8f1dc4414691c922dff558acff988492250be6a1f443a139b0e3762898  fail2ban-747d468-fix-tests.patch
994de8a4fdd4535607cd1b21553266de015b57bdb7f84f931973cb4b3cadd93fb2fda2d402a4ecccf505dffabf146cd9eae2cd0b635c3cb3dfa2d312539d41be  CVE-2021-32749.patch
acd8f1f97233ed02012b166c62e8dc67b89729b82f0fcd81c871d0e425633f617404390b8301e6b22ef3ddf137f604a47aae7880a9f4ba6601b8d0ffe52b4f85  python-3.10.patch
7af6a22b60e21557dfbfe2c473156ca22a8f27978c6897d4b3b050f737eeced9dc26921ae52b1de5faa399f89f5fe80364633a4a3c80b100fc99882fd55824b4  setuptools59.patch
1e7581dd04e7777d6fd5c40cc842a7ec5f4e6a0374673d020d89dd61bf4093d48934844bee89bcac9084f9ae44f3beb66e714cf3c2763d79c3e8feb790c5e43b  fail2ban.confd
ee1c229db970239ebc707cd484a650fcf2347c70b411728ee2a4a35a72f4118cfccecf2a221275603320e0332efcc16e4979201933cec1aef1c5d5a082fc4940  fail2ban.logrotate
84915967ae1276f1e14a5813680ee2ebf081af1ff452a688ae5f9ac3363f4aff90e39f8e6456b5c33d5699917d28a16308797095fd1ef9bb1fbcb46d4cea3def  alpine-ssh.jaild
3e8e08d5e349e857b51ce34a9d968f16661b34e1cec06bec0aa9a32723bbe9be5a9890dd479331a9cc860821d33b1bf3b8e995182e319dead5a3d434b1816304  alpine-sshd.filterd
36a81b771be0b36fe0dfb5ee4c72c9cb5b504e110618a8eb6f0f241b4e57d92df01dc5cc04b6b68d5bc6a5e6d68de1000092770285d7a328e5937e50b4b226a3  alpine-sshd-ddos.filterd
"
